﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TheLoader.Properties;

namespace TheLoader
{
    public partial class TesRecorder : Form
    {
        public Form1 myParent { get; set; }
        public TesRecorder()
        {
            InitializeComponent();
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            AddItemToList(webBrowser1.Url.ToString());
            webBrowser1.Stop();
            textBox1.Text = webBrowser1.Url.ToString();
            if (Settings.Default.VisitedUrls.IndexOf(webBrowser1.Url.ToString()) < 0)
            {
                Settings.Default.VisitedUrls += "|" + webBrowser1.Url.ToString();
                Settings.Default.Save();
            }

            ReloadtextBoxItems();

        }

        private void AddItemToList(string url)
        {
            bool exists = false;
            foreach (object o in listBox1.Items)
            {
                string s = (string)o;
                if (s == url)
                    exists = true;
            }

            if(!exists)
                listBox1.Items.Add(url);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            webBrowser1.Navigate(textBox1.Text);
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            LoadLinksToRTB();
            this.Close();
        }

        private void LoadLinksToRTB()
        {
            if (listBox1.Items.Count > 0)
            {
                myParent.rtb1.Text = "";
                string[] urls = new string[listBox1.Items.Count];
                int count = 0;
                foreach (object o in listBox1.Items)
                {
                    string s = (string)o;
                    urls[count] = s;
                    count++;
                }

                myParent.rtb1.Lines = urls;

            }
        }

        private void TesRecorder_Load(object sender, EventArgs e)
        {
            ReloadtextBoxItems();
        }

        private void ReloadtextBoxItems()
        {
            string[] urls = Settings.Default.VisitedUrls.Split('|');
            textBox1.Items.Clear();
            foreach (string s in urls)
            {
                if (s != "")
                    textBox1.Items.Add(s);
            }
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            Settings.Default.VisitedUrls = "";
            Settings.Default.Save();
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            sfd1.OverwritePrompt = true;
            sfd1.ShowDialog();
            


        }

        private void sfd1_FileOk(object sender, CancelEventArgs e)
        {
            
            if (listBox1.Items.Count > 0)
            {
                LoadLinksToRTB();
                                
                myParent.rtb1.SaveFile(sfd1.FileName,RichTextBoxStreamType.PlainText);
                this.Close();

            }

            
        }

        private void textBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            webBrowser1.Navigate(textBox1.Text);
        }

        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
           if( e.KeyData == Keys.Enter)
               webBrowser1.Navigate(textBox1.Text);
        }

       

       
    }
}
